<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="采集时间">
          <el-date-picker
            v-model="searchParam.collectTimeSearch"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="getcollectTime"
          />
        </el-form-item>
        <el-form-item label="数据文件名称">
          <el-input
            v-model="searchParam.collectFilename"
            placeholder="请输入数据文件名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="采集状态">
          <el-select
            v-model="searchParam.dfxFileStatus"
            placeholder="请选择采集状态"
            clearable
          >
            <el-option
              v-for="item in useDictionary('qdasFileStatus')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <!--      <template #header>-->
      <!--                  <el-button type="danger" plain :disabled="ids.length === 0" @click="handleTableDelete()"><i-ep-delete />删除</el-button>-->
      <!--                -->
      <!--      </template>-->

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <!--      <el-table-column type="selection" width="55" align="center" />-->
        <el-table-column
          label="采集时间"
          prop="lastUpdatedTime"
          min-width="70"
          show-overflow-tooltip
        />
        <el-table-column
          label="检具编号"
          prop="detectionEquipMCode"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="数据文件路径"
          prop="path"
          min-width="250"
          show-overflow-tooltip
        >
          <template #default="scope">
            {{
              scope.row.fileType === 1 ? scope.row.dfdRoute : scope.row.dfxRoute
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="数据文件名称"
          prop="collectFilename"
          min-width="100"
          show-overflow-tooltip
        >
          <template #default="scope">
            {{
              resolveFileName(
                scope.row.fileType === 1
                  ? scope.row.dfdRoute
                  : scope.row.dfxRoute
              )
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="采集记录数"
          prop="dfxLineIndex"
          min-width="50"
          show-overflow-tooltip
        />
        <el-table-column
          label="采集状态"
          prop="dfxFileStatus"
          min-width="50"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag :type="fileStatus[scope.row.dfxFileStatus]?.type">
              {{ fileStatus[scope.row.dfxFileStatus]?.text }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="采集处理信息"
          prop="dfxHandleMess"
          min-width="100"
          show-overflow-tooltip
        />
        <!--          <el-table-column fixed="right" label="操作" width="100" align="center">-->
        <!--          <template #default="scope">-->
        <!--            <el-button-->
        <!--                type="primary"-->
        <!--                size="small"-->
        <!--                link-->
        <!--                @click="deleteItem(scope.row.id)"><i-ep-delete />删除</el-button>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { last } from "lodash-es";
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "ETLQDQDASCOLLECTRECORD",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/pqd-etl/qdqdascollectrecord/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  collectTimeSearch: [],
  collectTimeStart: "",
  collectTimeEnd: "",
  collectFilename: "",
  collectStatus: "",
};

const deleteParam = {
  api: "/pqd-etl/qdqdascollectrecord/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

const fileStatus = [
  { text: "未执行", type: "info" },
  { text: "正常", type: "success" },
  { text: "异常", type: "danger" },
];

onMounted(() => {
  getTableList();
});

function getcollectTime(data: any) {
  if (data) {
    searchParam.value.lastUpdatedTimeStart = data[0];
    searchParam.value.lastUpdatedTimeEnd = data[1];
  } else {
    searchParam.value.lastUpdatedTimeStart = "";
    searchParam.value.lastUpdatedTimeEnd = "";
  }
}

function resolveFileName(path: string) {
  if (!path) return;
  return last(path.split("\\"));
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/pqd-etl/qdqdascollectrecord/deleteItem/${id}`,
      method: "post",
    });
    search();
  });
}
</script>
